import {Bounds} from '../utils'
// Get path area from an array of [x, y] points
// TODO: consider removing duplication with getPathArea(), e.g. by
//   wrapping points in an iterator.
//
function getPlanarPathArea2(points) {
  let sum = 0,
      ax, ay, bx, by, dx, dy, p
  for (let i = 0, n = points.length; i < n; i += 1) {
    p = points[i]
    if (i === 0) {
      ax = 0
      ay = 0
      dx = -p[0]
      dy = -p[1]
    } else {
      ax = p[0] + dx
      ay = p[1] + dy
      sum += ax * by - bx * ay
    }
    bx = ax
    by = ay
  }
  return sum / 2
}
function getPlanarPathArea(ids, arcs) {
  const iter = arcs.getShapeIter(ids)
  let sum = 0
  let ax, ay, bx, by, dx, dy
  if (iter.hasNext()) {
    ax = 0
    ay = 0
    dx = -iter.x
    dy = -iter.y
    while (iter.hasNext()) {
      bx = ax
      by = ay
      ax = iter.x + dx
      ay = iter.y + dy
      sum += ax * by - bx * ay
    }
  }
  return sum / 2
}
function getPathBounds(points) {
  const bounds = new Bounds()
  for (let i=0, n=points.length; i<n; i++) {
    bounds.mergePoint(points[i][0], points[i][1])
  }
  return bounds
}
export default {
  getPlanarPathArea2,
  getPlanarPathArea,
  getPathBounds,
}